package com.link.base.base.quote.service;


import com.link.base.base.order.dao.mybatis.mapper.SaleOrderItemMapper;
import com.link.base.base.order.dao.mybatis.mapper.SaleOrderMapper;
import com.link.base.base.order.model.SaleOrder;
import com.link.base.base.order.model.SaleOrderItem;
import com.link.base.base.quote.dao.mybatis.mapper.QuoteMapper;
import com.link.base.base.quote.model.Quote;
import com.link.base.base.quote.model.QuoteItem;
import com.link.core.basic.dao.KeyGenerateDao;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import javax.annotation.Resource;

import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.snowflake.GenerateCodeSingleton;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author unknown
 * @date unknown
 * @comments 报价单操作服务
 */
@Component
public class QuoteServiceImpl extends BasicServiceImpl<Quote> implements QuoteService {
    @Resource
    private QuoteMapper quoteMapper;

    @Resource
    private SaleOrderMapper saleOrderMapper;

    @Resource
    private SaleOrderItemMapper saleOrderItemMapper;

    @Resource
    private KeyGenerateDao keyGenerateDao;
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public BasicMapper<Quote> getBasicMapper() throws Exception {
        if (quoteMapper != null) {
            return quoteMapper;
        }
        throw new ServiceException("PUBLIC-006");
    }

    /**
     * 提交报价
     *
     * @throws Exception
     */
    @Override
    public void quoteSubmit(Quote record) throws Exception {
        record.setQuoteStatus("Submitted");
        quoteMapper.quoteSubmitUpdate(record);
    }

    /**
     * 审批通过报价
     *
     * @throws Exception
     */
    @Override
    public void quoteApprove(Quote record) throws Exception {
        record.setQuoteStatus("Approved");
        quoteMapper.quoteApproveUpdate(record);
    }

    /**
     * 审批拒绝报价
     *
     * @throws Exception
     */
    @Override
    public void quoteReject(Quote record) throws Exception {
        record.setQuoteStatus("Rejected");
        quoteMapper.quoteRejectUpdate(record);
    }

    /**
     * 取消报价
     *
     * @throws Exception
     */
    @Override
    public void quoteCancel(Quote record) throws Exception {
        record.setQuoteStatus("Cancelled");
        quoteMapper.quoteCancelUpdate(record);
    }

    /**
     * 报价转订单
     */
    @Override
    @Transactional(rollbackFor = BasicServiceException.class)
    public void quoteGenerateOrder(Quote record) throws Exception {
//        String orderNo = keyGenerateDao.keyGenerate().toString();
        //使用GenerateCodeSingleton获取订单编号
        GenerateCodeSingleton generateCodeSingleton = GenerateCodeSingleton.getInstance();
        String orderNo = generateCodeSingleton.getOrderCodeStr();
        Long orderRowId = keyGenerateDao.keyGenerate();

        SaleOrder order = new SaleOrder();
        //行Id
        order.setId(orderRowId);
        //订单编号
        order.setOrderNo(orderNo);
        order.setOrderType("销售订单");
        //客户Id
        order.setAcctId(record.getAcctId());
        order.setStatus("新建");
        order.setOrgId(record.getOrgId());
        order.setPostnId(record.getPostnId());
        if (order.getAcctId() == null) {
            throw new Exception("客户不能为空！");
        }
        //新建订单头
        saleOrderMapper.insert(order);

        if (record.getProdList() != null) {
            for (QuoteItem l : record.getProdList()) {
                SaleOrderItem op = new SaleOrderItem();
                Long productRowId = keyGenerateDao.keyGenerate();
                //头id
                op.setHeadId(orderRowId);
                //行ID
                op.setId(productRowId);
                //产品id
                op.setProdId(l.getProdId());
                //总金额
                op.setBasePrice(Double.valueOf(l.getPrice()));
                //产品个数
                op.setQty(Double.valueOf(l.getQty()));
                //新建订单行
                saleOrderItemMapper.insert(op);
            }
        }
    }
}

